package com.sw.youvote.server.persistentobjects;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;

import javax.jdo.Transaction;

import java.util.logging.Logger;

import com.sw.youvote.shared.objects.CandidatureBean;
import com.sw.youvote.shared.objects.QuizBean;

public class QuizJdoDAO implements QuizDAO {

	private static final Logger log = Logger.getLogger(QuizJdoDAO.class
			.getName());

	public void addQuiz(QuizBean quiz) {
		PersistenceManager pm = WocomPersistenceManager.getPersistenceManagerFactory()
				.getPersistenceManager();
		
		log.info("addQuiz::Bean para insertar =" + quiz);
		try {
			pm.makePersistent(quiz);
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<QuizBean> listQuizs() {
		PersistenceManager pm = WocomPersistenceManager.getPersistenceManagerFactory()
				.getPersistenceManager();

		String query = "select from " + QuizBean.class.getName();
		return (List<QuizBean>) pm.newQuery(query).execute();

		// log.info("listQuizs:: obtener todas los concursos =" + result);
		// return result;
	}

	public void removeQuiz(QuizBean quiz) {
		PersistenceManager pm = WocomPersistenceManager.getPersistenceManagerFactory()
				.getPersistenceManager();
		try {
			pm.currentTransaction().begin();

			// We don't have a reference to the selected Product.
			// So we have to look it up first,
			quiz = pm.getObjectById(QuizBean.class, quiz.getId());
			pm.deletePersistent(quiz);

			pm.currentTransaction().commit();
		} catch (Exception ex) {
			pm.currentTransaction().rollback();
			throw new RuntimeException(ex);
		} finally {
			pm.close();
		}
	}

	public void updateQuiz(QuizBean quiz) {
		PersistenceManager pm = WocomPersistenceManager.getPersistenceManagerFactory()
				.getPersistenceManager();
		
		QuizBean temp = null;
		String name = quiz.getName();
		String description = quiz.getDescription();
		String creatormail = quiz.getCreatorMail();
		String imageurl = quiz.getImageurl();
	
		try {
			pm.currentTransaction().begin();
			// We don't have a reference to the selected Product.
			// So we have to look it up first,
			temp = pm.getObjectById(QuizBean.class, quiz.getId());
			
			temp.setName(name);
			temp.setDescription(description);
			temp.setCreatorMail(creatormail);
			temp.setImageurl(imageurl);
			// No se actualiza las candidaturas
			
			pm.makePersistent(temp);
			pm.currentTransaction().commit();
		} catch (Exception ex) {
			pm.currentTransaction().rollback();
			throw new RuntimeException(ex);
		} finally {
			pm.close();
		}
	}

	@Override
	public void addCandidature(QuizBean quizObject, CandidatureBean candidature) {
		// PersistenceManager pm = getPersistenceManagerFactory()
		// .getPersistenceManager();

		// if (quiz != null && candidature != null) {

		// if (quiz.getCandidatureSets() == null)
		// quiz.setCandidatureSets(new ArrayList());

		// quiz.getCandidatureSets().add(candidature);

		// log.info("QuizBean antes de insertar " + quiz);
		// updateQuiz(quiz);

		// }

		PersistenceManager pm = WocomPersistenceManager.getPersistenceManagerFactory()
				.getPersistenceManager();
		
		QuizBean quiz = null;
		
		try {
			pm.currentTransaction().begin();
			// We don't have a reference to the selected Product.
			// So we have to look it up first,
			quiz = pm.getObjectById(QuizBean.class, quizObject.getId());
			
			if (quiz.getCandidatureSets()== null)
				 quiz.setCandidatureSets(new ArrayList());
			
			quiz.getCandidatureSets().add(candidature);
					
			
			pm.makePersistent(quiz);
			pm.currentTransaction().commit();
		} catch (Exception ex) {
			pm.currentTransaction().rollback();
			throw new RuntimeException(ex);
		} finally {
			pm.close();
		}

	}

	@Override
	public List<CandidatureBean> listCandidatures(QuizBean quiz) {
		PersistenceManager pm = WocomPersistenceManager.getPersistenceManagerFactory()
				.getPersistenceManager();

		List<CandidatureBean> candidatures = null;

		QuizBean result = null;

		if (quiz != null) {

			log.info("listCandidatures:: Buscando concurso id " + quiz.getId());

			result = pm.getObjectById(QuizBean.class, quiz.getId());

			log.info("listCandidatures:: Objeto concurso encontrado " + result);
			// String query = "select from " + QuizBean.class.getName()
			// + " where id ==" + quiz.getId();
			// result = (QuizBean) pm.newQuery(query).execute();

			if (result != null) {
				candidatures = result.getCandidatureSets();
				log
						.info("listCandidatures:: Candidaturas asociadas al concurso "
								+ candidatures);
			}
		}

		return candidatures;
	}

	@Override
	public void removeCandidature(QuizBean quiz, CandidatureBean candidature) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateCandidature(QuizBean quiz, CandidatureBean candidature) {
		// TODO Auto-generated method stub

	}

}
